package com.arron.principle.singleton.type7;

public class Singleton7 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance==instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
    }
}

/* 懒汉式（线程安全,静态内部类，类加载是线程安全的）推荐使用
* */
class Singleton{
    // 1、构造器私有化，外部能new
    private Singleton(){
    }

    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}